package top.superflow.exception

import java.util.regex.Pattern

trait Exceptable {

  val placeHolderPattern = Pattern.compile("\\{\\s*\\}");

  protected def throwRequestUnsupportedException(formattedMessage : String, args : Any*): Unit ={
    throw new RequestUnsupportedException(reformat(formattedMessage), args)
  }


  private def reformat(formattedMessage : String) : String = {
    var i = 0

    var reformattedMessage = formattedMessage
    var matcher = placeHolderPattern.matcher(reformattedMessage)
    while(matcher.find()){
      reformattedMessage = matcher.replaceFirst("{" + i + "}")
      matcher = placeHolderPattern.matcher(reformattedMessage)
      i += 1
    }
    reformattedMessage
  }
}

object Exceptable{
  def main(args: Array[String]): Unit = {
    val placeHolderPattern = Pattern.compile("\\{\\s*\\}");
    print(reformat("abaadfadf{ }, eeeee {}", placeHolderPattern))
  }

  private def reformat(formattedMessage : String, placeHolderPattern : Pattern) : String = {
    var i = 0

    var reformattedMessage = formattedMessage
    var matcher = placeHolderPattern.matcher(reformattedMessage)
    while(matcher.find()){
      reformattedMessage = matcher.replaceFirst("{" + i + "}")
      matcher = placeHolderPattern.matcher(reformattedMessage)
      i += 1
    }
    reformattedMessage
  }
}
